package com.koron.css2.clientCenter.impl;


import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserInfoSplitDetail;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoSplitDetailMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoSplitDetailQuery;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BusinessAreaMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据证件号码查询相关的用户数据
 *
 * @author 朱超
 * @since 2021-12-24 15:15:08
 */
@Service
public class UserInfoSplitDetailListByCertNoAndBusinessArea implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            UserInfoSplitDetailMapper userInfoSplitDetailMapper = factory.getMapper(UserInfoSplitDetailMapper.class);
            BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);

            // 1. 根据请求json获取实例bean
            UserInfoSplitDetailQuery userInfoSplitDetailQuery = JsonUtils.objectToPojo(req.getData(), UserInfoSplitDetailQuery.class);
            // 2. 根据certNo和businessArea查出对应数据（user_no,setup_meter_addr,ctm_no,account_no）
            List<UserInfoSplitDetail> userInfoSplitDetailList = userInfoSplitDetailMapper.getListFromUserInfo(userInfoSplitDetailQuery);
            // 3. 查询pub_business_area中的id和area的对应map
            Map<String, Map> areaNameIdMap = new HashMap<>();
            areaNameIdMap = businessAreaMapper.getNameAndId();
            // 4. 查出ctm_name, ctm_addr, businessAreaName
            for (UserInfoSplitDetail userInfoSplitDetail : userInfoSplitDetailList) {
                // 4.1 根据ctm_no查询ctm_name, ctm_addr
                CtmInfoBean ctmInfoBean = ctmInfoMapper.getCtmInfoByCtmNo(userInfoSplitDetail.getCtmNo(), 1);
                userInfoSplitDetail.setCtmAddr(ctmInfoBean.getCtmAddr());
                userInfoSplitDetail.setCtmName(ctmInfoBean.getCtmName());
                // 4.2 根据businessArea获取businessAreaName
                String businessArea = userInfoSplitDetail.getBusinessArea();
                Map<String, Object> singleMap = areaNameIdMap.get(businessArea);
                if (singleMap != null && !singleMap.isEmpty()) {
                    userInfoSplitDetail.setBusinessAreaName(singleMap.get("area_name").toString());
                }
            }
            // 5. 返回结果
            MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "根据证件号码查询并户详情成功", List.class);
            msg.setData(userInfoSplitDetailList);
            return msg;
        } catch (Exception e) {
            logger.error("根据证件号码查询并户详情失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "根据证件号码查询并户详情失败", void.class);
        }
    }

}
